<html>
<head>
<script type="text/javascript">
	function Insert() {
		document.getElementById("meetingstarttime").required = true;
		document.getElementById("meetingendtime").required = true;
		document.getElementById("action").value = "insert";
	}
	function Update(row) {
		document.getElementById("meetingstarttime" + row).required = true;
		document.getElementById("meetingendtime" + row).required = true;
		document.getElementById("action").value = "update";
		document.getElementById("row").value = row;
		console.log("Update " + row);
	}
	function Delete(row) {
		document.getElementById("action").value = "delete";
		document.getElementById("row").value = row;

	}
</script>
</head>
<body>
	<%@ page language="java" import="java.sql.*"%>
	<%
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/cse132b","cse132blogin", "1234567890");
			String action = request.getParameter("action");
			// Check if an insertion is requested
			if (action != null && action.equals("insert")) {
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("INSERT INTO meetings (section_id,meeting_type,meeting_day,meeting_start_time,meeting_end_time,required) VALUES (?,?,?,?,?,?)");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("sectionid")));
				pstmt.setString(2, request.getParameter("meetingtype"));
				pstmt.setString(3, request.getParameter("meetingday"));
				String arr[] = request.getParameter("meetingstarttime").split(":");
				Time time = new Time(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),0);
				pstmt.setTime(4,time);
				arr = request.getParameter("meetingendtime").split(":");
				time = new Time(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),0);
				pstmt.setTime(5,time);
				pstmt.setBoolean(6, (request.getParameter("required") != null && request.getParameter("required").equals("t")) ? true : false);

				int rowCount = pstmt.executeUpdate();
				out.println("Successfully made new section.<br/>");
			} else if (action != null && action.equals("update")) {
				int edit = Integer.parseInt(request.getParameter("row"));
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("UPDATE meetings SET meeting_type = ?,meeting_day = ?,meeting_start_time = ?,meeting_end_time = ?,required = ? WHERE section_id = ?");
				pstmt.setString(1, request.getParameter("meetingtype" + edit));
				pstmt.setString(2, request.getParameter("meetingday" + edit));
				String arr[] = request.getParameter("meetingstarttime" + edit).split(":");
				Time time = new Time(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),0);
				pstmt.setTime(3,time);
				arr = request.getParameter("meetingendtime" + edit).split(":");
				time = new Time(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),0);
				pstmt.setTime(4,time);
				pstmt.setBoolean(5, (request.getParameter("required" + edit) != null && request.getParameter("required" + edit).equals("t")) ? true : false);
				pstmt.setInt(6, Integer.parseInt(request.getParameter("sectionid")));
				int rows = pstmt.executeUpdate();
			} else if (action != null && action.equals("delete")) {
				int edit = Integer.parseInt(request.getParameter("row"));
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("DELETE FROM meetings WHERE meeting_id = ?");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("row")));
				pstmt.executeUpdate();
			}

			// Create the statement
			PreparedStatement meetings = conn.prepareStatement("SELECT * FROM meetings WHERE section_id = ?");
			meetings.setInt(1, Integer.parseInt(request.getParameter("sectionid")));
			// Use the created statement to SELECT
			// the student attributes FROM the Student table.
			ResultSet rsExistingMeetings = meetings.executeQuery();
			%>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>	
	<table border="0">
		<tr>
			<td valign="top">Sections Entry Page</td>
		</tr>
		<tr>
			<form action="meetings.jsp" method="post">
			<input type="hidden" id="action" name="action"/>
			<input type="hidden" id="row" name="row"/>
			<input type="hidden" id="sectionid" name="sectionid" value="<%=request.getParameter("sectionid") %>"/>
			<table border="1">
				<tr>
					<th>Meeting Type</th>
					<th>M</th>
					<th>T</th>
					<th>W</th>
					<th>Th</th>
					<th>F</th>
					<th>Start Time</th>
					<th>End Time</th>
					<th>Required</th>
				</tr>
				<tr>
					<td><select id="meetingtype" name="meetingtype">
						<option value="lecture">Lecture</option>
						<option value="discussion">Discussion</option>
					</select></td>
					<td><input type="radio" name="meetingday" value="Monday"/></td>
					<td><input type="radio" name="meetingday" value="Tuesday"/></td>
					<td><input type="radio" name="meetingday" value="Wednesday"/></td>
					<td><input type="radio" name="meetingday" value="Thursday"/></td>
					<td><input type="radio" name="meetingday" value="Friday"/></td>
					<td><input type="time" id="meetingstarttime" name="meetingstarttime"/></td>
					<td><input type="time" id="meetingendtime" name="meetingendtime"/></td>					
					<td><input type="checkbox" name="required" value="t"/></td>
					<td><input type="submit" onclick="Insert()" value="Add New"/></td>
				</tr>
				<%
				while(rsExistingMeetings.next()) {
				%>
				<tr>
					<td><select id="meetingtype<%=rsExistingMeetings.getInt("meeting_id") %>" name="meetingtype<%=rsExistingMeetings.getInt("meeting_id") %>">
						<option value="lecture" <%=(rsExistingMeetings.getString("meeting_type").equals("lecture") ? "selected" : "") %>>Lecture</option>
						<option value="discussion" <%=(rsExistingMeetings.getString("meeting_type").equals("discussion") ? "selected" : "") %>>Discussion</option>
					</select></td>
					<td><input type="radio" name="meetingday<%=rsExistingMeetings.getInt("meeting_id") %>" value="Monday" <%=(rsExistingMeetings.getString("meeting_day").equals("Monday") ? "checked" : "") %>/></td>
					<td><input type="radio" name="meetingday<%=rsExistingMeetings.getInt("meeting_id") %>" value="Tuesday" <%=(rsExistingMeetings.getString("meeting_day").equals("Tuesday") ? "checked" : "") %>/></td>
					<td><input type="radio" name="meetingday<%=rsExistingMeetings.getInt("meeting_id") %>" value="Wednesday" <%=(rsExistingMeetings.getString("meeting_day").equals("Wednesday") ? "checked" : "") %>/></td>
					<td><input type="radio" name="meetingday<%=rsExistingMeetings.getInt("meeting_id") %>" value="Thursday" <%=(rsExistingMeetings.getString("meeting_day").equals("Thursday") ? "checked" : "") %>/></td>
					<td><input type="radio" name="meetingday<%=rsExistingMeetings.getInt("meeting_id") %>" value="Friday" <%=(rsExistingMeetings.getString("meeting_day").equals("Friday") ? "checked" : "") %>/></td>
					<td><input type="time" id="meetingstarttime<%=rsExistingMeetings.getInt("meeting_id") %>" name="meetingstarttime<%=rsExistingMeetings.getInt("meeting_id") %>" value="<%=rsExistingMeetings.getTime("meeting_start_time") %>"/></td>
					<td><input type="time" id="meetingendtime<%=rsExistingMeetings.getInt("meeting_id") %>" name="meetingendtime<%=rsExistingMeetings.getInt("meeting_id") %>" value="<%=rsExistingMeetings.getTime("meeting_end_time") %>"/></td>
					<td><input type="checkbox" name="required<%=rsExistingMeetings.getInt("meeting_id") %>" value="t" <%=(rsExistingMeetings.getBoolean("required") ? "checked" : "") %>/></td>
					<td><input type="submit" onclick="Update(<%=rsExistingMeetings.getInt("meeting_id") %>)" value="Update"/><input type="submit" onclick="Delete(<%=rsExistingMeetings.getInt("meeting_id") %>)" value="Delete"/></td>
				</tr>
				<%
				}
				%>
			</table>
		</tr>
	</table>
	</form>
	<%
		} catch (SQLException ex) {
			out.print("SQLException: " + ex.getMessage() + "<br/>");
			out.print("SQLState: " + ex.getSQLState() + "<br/>");
			out.print("VendorError: " + ex.getErrorCode() + "<br/>");
		}
	%>
</td>
</tr>
</table>
</body>
</html>